package april.test_4_14;

import java.util.ArrayList;

public class Solution {
    public int[] StringAssemble (String destStr, String[] elements) {
        // write code here
        ArrayList<Integer> arrayList = new ArrayList<>();
        int indexof = 0;
        while (indexof < destStr.length()) {
            for (int i = 0; i < elements.length; i++) {
                if(destStr.indexOf(elements[i],indexof) == indexof) {
                    arrayList.add(i);
                    indexof += elements[i].length();
                }
            }
        }
        int[] arr = new int[arrayList.size()];
        for (int i = 0; i < arrayList.size(); i++) {
            arr[i] = arrayList.get(i);
        }
        return arr;
    }
    public int getIsLandGroupCount (int[][] connectedArray) {
        // write code here
        int m = connectedArray.length;
        int n = connectedArray[0].length;
        if( n == 0) {
            return 0;
        }
        int[][] arr = new int[m + 1][n + 1];
        int result = 0;
        ArrayList<Integer> arrayList = new ArrayList<>();
        for (int i = 1; i <= m; i++) {
            for (int j = 1; j <= n; j++) {
                arr[i][j] = connectedArray[i - 1][j - 1];
                if(arr[i][j] == 1) {
                    result++;
                    arrayList.add(i);
                    arrayList.add(j);
                    if(arr[i - 1][j] == 1 && arrayList.contains(i - 1)) {
                        result--;
                    }
                    if(arr[i][j - 1] == 1 && arrayList.contains(j - 1)) {
                        result--;
                    }
                }
            }
        }
        return result;
    }
}
